#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "lunar.h"

void lunar(char *in_date, char *out_date)
{
    int i, int_date;
    int date[8];
    char buff[100];
    /*for(i=0;i<8;i++)
    {
        date[i] = in_date[i] - '0';
    }*/
    int_date = atoi(in_date);

    int year, month, day;
    year = int_date / 10000;
    month = (int_date - year * 10000) / 100;
    day = (int_date - year * 10000 - month * 100);

    /*
    year =date[0]*1000 + date[1]*100 +date[2]*10 + date[3];
    month =date[4]*10 + date[5];
    day =date[6]*10 +date[7];
    */
    year = year - 1; //年减1

    //月减2
    if (month < 3)
    {
        year -= 1;
        month = 12 - 2 + month;
    }
    else
    {
        month -= 2;
    }

    //日减5
    if (day < 6)
    {
        if (month == 1)
        {
            year -= 1;
            month = 12;
            day = 30 - 5 + day;
        }
        else
        {
            month -= 1;
            day = 30 - 5 + day;
        }
    }
    else
    {
        day -= 5;
    }

    date[0] = year / 1000;
    date[1] = (year - date[0] * 1000) / 100;
    date[2] = (year - date[0] * 1000 - date[1] * 100) / 10;
    date[3] = (year - date[0] * 1000 - date[1] * 100 - date[2] * 10);
    date[4] = month / 10;
    date[5] = month - date[4] * 10;
    date[6] = day / 10;
    date[7] = day - date[6] * 10;

    for (i = 0; i < sizeof(date) / sizeof(int); i++)
    {
        sprintf(buff, "%d", date[i]);
        strcat(out_date, buff);
    }
}